package cn.wowjoy.pda.android_pharmacy.ui.activity.door.merge;

import android.content.Intent;
import android.util.Log;

import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import cn.wowjoy.pda.android_pharmacy.api.LoginService;
import cn.wowjoy.pda.android_pharmacy.api.MergeService;
import cn.wowjoy.pda.android_pharmacy.common.core.BasePresenter;
import cn.wowjoy.pda.android_pharmacy.common.core.http.ServiceFactory;
import cn.wowjoy.pda.android_pharmacy.common.utils.Contants;
import cn.wowjoy.pda.android_pharmacy.common.utils.PreferenceManager;
import cn.wowjoy.pda.android_pharmacy.common.widget.CreateDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MDialog;
import cn.wowjoy.pda.android_pharmacy.model.commonEntity.DoorMergeEntity;
import cn.wowjoy.pda.android_pharmacy.model.commonEntity.DoorMergeQueryEntity;
import cn.wowjoy.pda.android_pharmacy.model.commonEntity.DoorMergeResultEntity;
import cn.wowjoy.pda.android_pharmacy.model.commonEntity.LoginInfo;
import cn.wowjoy.pda.android_pharmacy.model.commonEntity.MergeVORequest;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.doorPatientInfo.DoorPatientInfoBeen;
import cn.wowjoy.pda.android_pharmacy.ui.activity.door.merge.detail.DoorMergeDetailActivity;
import cn.wowjoy.pda.android_pharmacy.ui.activity.door.take.DoorTakeMedicineDetailActivity;
import cn.wowjoy.pda.android_pharmacy.ui.adapter.DoorMergeIndexAdapter;
import gear.yc.com.gearlibrary.rxjava.rxbus.RxBus;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2018/5/21.
 */

public class DoorMergePresenter extends BasePresenter<DoorMergeViewModel>{

    public DoorMergeIndexAdapter mDoorMergeIndexAdapter = new DoorMergeIndexAdapter(activity);
    public LRecyclerViewAdapter adapter = new LRecyclerViewAdapter(mDoorMergeIndexAdapter);
    private MergeService mMergeService;
    private LoginService mLoginService;
    private MDialog waitDialog;
    private String patientId;

    public List<DoorMergeEntity> mDoorMergeEntities = new ArrayList<>();

    @Override
    public void onCreatePresenter() {
        mMergeService = (MergeService) ServiceFactory.getInstance().getServiceMerge(MergeService.class);
        mLoginService = (LoginService) ServiceFactory.getInstance().getServiceLogin(LoginService.class);

    }

    public void getOrderInfo(String number){
        Disposable disposable = mMergeService.getOrderInfo(number)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableObserver<DoorMergeEntity>() {
                    @Override
                    protected void onStart() {
                        if(null == waitDialog){
                            waitDialog = CreateDialog.waitingDialog(activity);
                        }
                    }

                    @Override
                    public void onNext(DoorMergeEntity xmlData) {
                        CreateDialog.dismiss(activity);
//                        if(null == mDoorMergeEntities){
//                            mDoorMergeEntities = new ArrayList<>();
//                        }
//                        mDoorMergeEntities.add(xmlData);
                        if(null != xmlData && 200!= xmlData.getState() && null!= xmlData.getMsg()){
                            RxBus.getInstance().post(Contants.DOOR_MERGE_EOORO,xmlData.getMsg());
                            return;
                        }
                        if(null != xmlData && null!= xmlData.getResults()){
                            RxBus.getInstance().post(Contants.DOOR_MERGE_ORDERINFO,xmlData);
                        }


                    }
                    @Override
                    public void onComplete() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        CreateDialog.dismiss(activity);
                        if(null != e && null != e.getMessage()){
                            Log.e("Throwable", e.getMessage());
                            RxBus.getInstance().post(Contants.DOOR_MERGE_EOORO,Contants.DOOR_MERGE_EOORO+e.getMessage());
                        }
                    }
                });
        addDisposable(disposable);
    }
    public void jump(DoorMergeEntity entity){
        Intent intent = new Intent(activity,DoorMergeDetailActivity.class);
        intent.putExtra("entity",entity);
        intent.putExtra("type",1);
        activity.startActivity(intent);
    }
    public void merge(MergeVORequest request){
        Disposable disposable = mMergeService.merge(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableObserver<DoorMergeResultEntity>() {
                    @Override
                    protected void onStart() {
                        if(null == waitDialog){
                            waitDialog = CreateDialog.waitingDialog(activity);
                        }
                    }

                    @Override
                    public void onNext(DoorMergeResultEntity xmlData) {
                        CreateDialog.dismiss(activity);
                        query(xmlData.getResults());

                    }
                    @Override
                    public void onComplete() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        CreateDialog.dismiss(activity);
                        if(null != e && null != e.getMessage()){
                            Log.e("Throwable", e.getMessage());
//                            new MyToast(activity).showinfo(e.getMessage());
                        }
                    }
                });
        addDisposable(disposable);
    }
    public void query(String mergeId){
        Disposable disposable = mMergeService.getMedicineByMergeId(mergeId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableObserver<DoorMergeQueryEntity>() {
                    @Override
                    protected void onStart() {
                        if(null == waitDialog){
                            waitDialog = CreateDialog.waitingDialog(activity);
                        }
                    }
                    @Override
                    public void onNext(DoorMergeQueryEntity responseBody) {
                        CreateDialog.dismiss(activity);
//                        Log.e("PXY", "onNext: "+responseBody.toString());
                        if(null != responseBody && responseBody.getMedicines().size()>0){
                            Intent intent = new Intent(activity, DoorTakeMedicineDetailActivity.class);
                            for(DoorPatientInfoBeen d : responseBody.getMedicines()){
                                if(null != mDoorMergeIndexAdapter.datas.get(0).getResults().getPrescription().getCfkdks()){
                                    d.setCFKDKS(mDoorMergeIndexAdapter.datas.get(0).getResults().getPrescription().getCfkdks());
                                }
                                if(null != mDoorMergeIndexAdapter.datas.get(0).getResults().getPrescription().getBrdaxm()){
                                    d.setBRDAXM(mDoorMergeIndexAdapter.datas.get(0).getResults().getPrescription().getBrdaxm());
                                }
                                if(null != mDoorMergeIndexAdapter.datas.get(0).getResults().getPrescription().getCfkdsj()){
                                    d.setCFKDSJ(mDoorMergeIndexAdapter.datas.get(0).getResults().getPrescription().getCfkdsj());
                                }
                                if(null != d.getZzypmc()){
                                    d.setZZYPMC(d.getZzypmc());
                                }
                                if(null != d.getCfypsl()){
                                    d.setCFYPSL(d.getCfypsl());
                                }
                                if(null != d.getKfwzmc()){
                                    d.setKFWZMC(d.getKfwzmc());
                                }
                                if(null != d.getYpspbm()){
                                    d.setYPSPBM(d.getYpspbm());
                                }
                                if(null != d.getYppmgg()){
                                    d.setYPPMGG(d.getYppmgg());
                                }
                                if(null != d.getYpjgbm()){
                                    d.setYPJGBM(d.getYpjgbm());
                                }
                                if(null != d.getZzypgg()){
                                    d.setZZYPGG(d.getZzypgg());
                                }
                                if(null != d.getYpbzdw()){
                                    d.setYPBZDW(d.getYpbzdw());
                                }
                                if(null != d.getYpcdid()){
                                    d.setYPCDID(d.getYpcdid());
                                }
                                if(null != d.getKfwzid()){
                                    d.setKFWZID(d.getKfwzid());
                                }
                                if(null != d.getYpcdmc()){
                                    d.setYPCDMC(d.getYpcdmc());
                                }
                                if(null != d.getKfmkid()){
                                    d.setKFMKID(d.getKfmkid());
                                }
                                if(null != d.getSfgwpb()){
                                    d.setSFGWPB(d.getSfgwpb());
                                }
                                if(null != d.getSflcpb()){
                                    d.setSFLCPB(d.getSflcpb());
                                }
                            }
                            intent.putExtra("DJJLIDs", (Serializable) mDoorMergeIndexAdapter.DJJLIDS);
                            intent.putExtra("type",1);
                            intent.putExtra("BRDAID","null");
                            intent.putExtra("BRZYID","0");
                            intent.putExtra("doorPatientInfoBeen",(Serializable) responseBody.getMedicines());
                            activity.startActivity(intent);
                            activity.finish();
                        }else {
                            Log.e("PXY", "onNext: "+responseBody.toString());
                            RxBus.getInstance().post(Contants.DOOR_MERGE_EOORO,Contants.DOOR_MERGE_EOORO);
                        }

                    }
                    @Override
                    public void onComplete() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        CreateDialog.dismiss(activity);
                        if(null != e && null != e.getMessage()){
                            Log.e("Throwable", e.getMessage());
//                            new MyToast(activity).showinfo(e.getMessage());
                        }
                    }
                });
        addDisposable(disposable);
    }
    public void getToken(){
        Disposable disposable = mLoginService.loginw("client_credentials")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableObserver<LoginInfo>() {
                    @Override
                    protected void onStart() {
                        if(null == waitDialog){
                            waitDialog = CreateDialog.waitingDialog(activity);
                        }
                    }

                    @Override
                    public void onNext(LoginInfo xmlData) {
                        CreateDialog.dismiss(activity);
                        PreferenceManager.getInstance().setCurrentUserAccessToken(xmlData.getAccessToken());

                    }
                    @Override
                    public void onComplete() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        CreateDialog.dismiss(activity);
                        if(null != e && null != e.getMessage()){
                            Log.e("Throwable", e.getMessage());
                            RxBus.getInstance().post(Contants.DOOR_MERGE_EOORO,e.getMessage());

                        }
                    }
                });
        addDisposable(disposable);
    }

//    public void mergeAndQuery(MergeVORequest request) {
//        Disposable disposable = mMergeService.merge(request)
//                .flatMap(new Function<DoorMergeResultEntity, Publisher<DoorMergeQueryEntity>>() {
//                    @Override
//                    public Publisher<DoorMergeQueryEntity> apply(DoorMergeResultEntity entity) throws Exception {
//                        if (null != entity && !"".equals(entity.getResults())) {
//                            return mMergeService.getMedicineByMergeId(entity.getResults());
//                        } else
//                            return Flowable.empty();
//                    }
//                })
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribeWith(new DisposableSubscriber<DoorMergeQueryEntity>() {
//                    @Override
//                    protected void onStart() {
//                        if(null == waitDialog){
//                            waitDialog = CreateDialog.waitingDialog(activity);
//                        }
//                    }
//
//                    @Override
//                    public void onNext(DoorMergeQueryEntity responseBody) {
//                        CreateDialog.dismiss(activity);
//                        //TODO：跳转传值
//                        Log.e("PXY", "onNext: "+responseBody.toString());
//                        if(null != responseBody && responseBody.getMedicines().size()>0){
//                            Intent intent = new Intent(activity, DoorTakeMedicineDetailActivity.class);
//                            intent.putExtra("DJJLID",responseBody.getMedicines().get(0).getDJJLID());
//                            intent.putExtra("BRDAID","null");
//                            intent.putExtra("BRZYID","0");
//                            intent.putExtra("doorPatientInfoBeen",(Serializable) responseBody.getMedicines());
//                            activity.startActivity(intent);
//                            activity.finish();
//                        }else {
//                            Log.e("PXY", "onNext: "+responseBody.toString());
//                            RxBus.getInstance().post(Contants.DOOR_MERGE_EOORO,Contants.DOOR_MERGE_EOORO);
//                        }
//
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        CreateDialog.dismiss(activity);
//                        if(null != e && null != e.getMessage()){
//                            Log.e("Throwable", e.getMessage());
//                            RxBus.getInstance().post(Contants.DOOR_MERGE_EOORO,Contants.DOOR_MERGE_EOORO+e.getMessage());
//                        }
//                    }
//
//                    @Override
//                    public void onComplete() {
//
//                    }
//                });
//        addDisposable(disposable);
//    }


}

